package com.ys.controller.api.overseas;

import com.ys.config.MenuId;
import com.ys.entity.overseas.TosPurchasePlan;
import com.ys.service.overseas.TosPurchasePlanService;
import com.ys.service.sys.CommonApiService;
import com.ys.util.admin.ConditionUtil;
import com.ys.util.shiro.ShiroUtils;
import com.ys.vo.LayuiTableVoJA;
import com.ys.vo.R;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * @Author: fxz
 * @CreateTime: 2024-12-18
 * @Description: HW采购计划 控制层
 * @Version: 1.5
 */
@CrossOrigin
@Controller
@RequestMapping("/api/inter/tosPurchasePlan")
public class TosPurchasePlanController {
    private static final Logger logger = LoggerFactory.getLogger(TosPurchasePlanController.class);


    @Autowired
    private TosPurchasePlanService tosPurchasePlanService;

    @Autowired
    private CommonApiService<TosPurchasePlan> commonApiService;

    Long MENUID = MenuId.TOS_PurchasePlan;

    public TosPurchasePlan setSeachSql(TosPurchasePlan cond){

        cond.addLeftJoin("TS_UserChange U ON U.Id = A.CreateChangeId ")
                .addField("U.UserId + '/' + U.UserName AS createUser")//创建人
                .addLeftJoin("TOS_PurchasePlanSon B ON B.pbid = A.bid")
                .addField("B.code,B.productImg,B.code,B.productName,B.purchaseAmount,B.deliveryAmount,B.purchaseNum,B.stockNum,B.orderCode,B.consignee,B.addressLine,B.city,B.postalCode,B.phone,B.id purchasePlanSonId")//子表商品信息
                .addLeftJoin("YS_ProcInst L ON L.Bid = A.Bid ")
                .addLeftJoin("TOS_PlatformInformation C ON B.platformId = C.id")
                .addField("C.platformCode,C.hwName,C.regionName,C.currencyCode")//平台信息
                .addLeftJoin("TOS_PurchasePlanItem D ON D.PurchasePlanSonId = B.id")
                .addField("D.tracingCode")
                .addField("B.status _ys_status")
                .addField("FORMAT(L.UpdateTime, 'yyyy-MM-dd HH:mm:ss') AS auditingTime")//审核时间
        ;
        return cond;
    }

    @RequiresPermissions("tosPurchasePlan:view")
    @RequestMapping("/search")
    @ResponseBody
    public LayuiTableVoJA search(Model model, HttpServletRequest request, HttpServletResponse response,
                                 @ModelAttribute TosPurchasePlan cond,String conditions
    ){

        setSeachSql(cond);
        String orderStatus = ConditionUtil.getConditionValue(conditions, "_ys_status");//订单状态
        if (orderStatus == null || "".equals(orderStatus)){
            cond.where("B.status = 1");
        }else if(!orderStatus.equals("7")){
            cond.where("B.status  = "+orderStatus);
        }
        cond = commonApiService.dataAuth(MENUID, cond);
        return commonApiService.searchLay(cond);
    }

    /**
     * 新增
     */
    @RequiresPermissions("tosPurchasePlan:add")
    @RequestMapping("/toAdd")
    @ResponseBody
    public R toAdd(HttpServletRequest request, @RequestParam Map<String, Object> params, String subData
    ) {
        try {
            logger.info("HW采购计划新增传参：" + params);
            //修改
            return tosPurchasePlanService.add(params,subData);
        } catch (Exception e) {
            logger.error("HW采购计划新增出错：", e.getMessage(), e);
            return R.error(e.getMessage());
        }
    }

    /**
     * 修改
     */
    @RequiresPermissions("tosPurchasePlan:add")
    @RequestMapping("/toEdit")
    @ResponseBody
    public R toEdit(HttpServletRequest request, @RequestParam Map<String, Object> params, String subData
    ) {
        try {
            logger.info("HW采购计划修改传参：" + params);
            //修改
            return tosPurchasePlanService.edit(params,subData);
        } catch (Exception e) {
            logger.error("HW采购计划修改出错：", e.getMessage(), e);
            return R.error(e.getMessage());
        }
    }

    /**
     * 删除
     */
    @RequiresPermissions("tosPurchasePlan:add")
    @RequestMapping("/deleteTrue")
    @ResponseBody
    public R del(HttpServletRequest request, Long id){
        try {
            return tosPurchasePlanService.del(id);
        } catch (Exception e) {
            logger.error("删除HW采购计划出错：",e.getMessage(),e);
            return R.error(e.getMessage());
        }
    }

    @RequiresPermissions("tosPurchasePlan:add")
    @PostMapping("/flowPass")
    @ResponseBody
    public R flowPass(HttpServletRequest request, Long id){
        try {
            return tosPurchasePlanService.flowPass(id, ShiroUtils.getUserId());
        } catch (Exception e) {
            logger.error("HW采购计划审批出错：",e.getMessage(),e);
            return R.error(e.getMessage());
        }
    }

    /**
     * 流转退回
     */
    @RequiresPermissions("tosPurchasePlan:add")
    @PostMapping("/flowRefuse")
    @ResponseBody
    public R flowRefuse(HttpServletRequest request, Long id, String reason){
        try {
            return tosPurchasePlanService.flowRefuse(id, reason, ShiroUtils.getUserId());
        } catch (Exception e) {
            logger.error("HW采购计划退回出错：",e.getMessage(),e);
            return R.error(e.getMessage());
        }
    }

    /**
     * 流转驳回
     */
    @RequiresPermissions("tosPurchasePlan:add")
    @PostMapping("/flowInit")
    @ResponseBody
    public R flowInit(HttpServletRequest request, Long id, String reason){
        try {
            return tosPurchasePlanService.flowInit(id, reason, ShiroUtils.getUserId());
        } catch (Exception e) {
            logger.error("HW采购计划驳回出错：",e.getMessage(),e);
            return R.error(e.getMessage());
        }
    }

    /**
     * 计划取消
     */
    @RequiresPermissions("tosPurchasePlan:qx")
    @PostMapping("/qx")
    @ResponseBody
    public R qx(HttpServletRequest request, Long id ){
        try {
            return tosPurchasePlanService.qx(id);
        } catch (Exception e) {
            logger.error("HW采购计划取消出错：",e.getMessage(),e);
            return R.error(e.getMessage());
        }
    }

    /**
     * 计划审核
     */
    @RequiresPermissions("tosPurchasePlan:sh")
    @PostMapping("/sh")
    @ResponseBody
    public R sh(HttpServletRequest request, @RequestParam Map<String, Object> params){
        try {
            return tosPurchasePlanService.sh(params);
        } catch (Exception e) {
            logger.error("HW采购计划审核出错：",e.getMessage(),e);
            return R.error(e.getMessage());
        }
    }
}
